#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "../../app/app_common.h"

int sqr(int n)
{
    int sum = 0;
    sum = n * n;
    return sum;
}

void *sqr_thread(void *arg)
{
    int num = *(int*)arg;
    int *sum = malloc(sizeof(int));
    *sum = sqr(num);
    sleep(1);
    return sum;
}

int main(int argc, char const *argv[])
{
    int items[5] = {10, 20, 30, 40, 50};
    int sum = 0;
    pthread_t tids[5] = {0};
    long start = App_Common_GetCurrentTime();
    for (int i = 0; i < (sizeof(items) / sizeof(int)); i++)
    {
        pthread_create(&tids[i], NULL, sqr_thread, &items[i]);
    }

    void *result = NULL;
    for (int i = 0; i < (sizeof(items) / sizeof(int)); i++)
    {
        pthread_join(tids[i], &result);
        sum += *(int*)result;
        free(result);
    }
    long end = App_Common_GetCurrentTime();
    printf("sum:%d,time:%ld\n", sum, (end - start) / 1000);
    return 0;
}
